package com.biaoyoo.www.personapp.widget;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import com.biaoyoo.www.personapp.R;
import com.biaoyoo.www.personapp.core.BaseListDefineActivity;
import com.biaoyoo.www.personapp.widget.pull.ILayoutManager;

/**
 * <pre>
 *     author : 40575
 *     e-mail : huyongxi2015@gmail.com
 *     time   : 2017/05/24
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class PullToRefreshRecycler extends FrameLayout implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecycleView;

    public static final int ACTION_PULL_TO_REFRESH = 1;
    public static final int ACTION_LOAD_MORE = 1 << 1;
    public static final int ACTION_IDLE = 1 << 2;
    private int mCurrentState = ACTION_IDLE;

    private OnRecycleRefreshListener mListener;

    /**
     * 是否加载更多的开关
     */
    private boolean mIsLoadMoreEnabled = false;
    private boolean mIsPullToRreshEnabled = false;
    private ILayoutManager mManager;

    public void setmAdapter(BaseListDefineActivity.BaseListDefineAdapter mAdapter) {
        this.mAdapter = mAdapter;
        mRecycleView.setAdapter(mAdapter);
    }

    private BaseListDefineActivity.BaseListDefineAdapter mAdapter;

    public PullToRefreshRecycler(@NonNull Context context) {
        this(context,null,0);
    }

    public PullToRefreshRecycler(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PullToRefreshRecycler(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    /**
     * 初始化view
     */
    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.widget_pull_to_refresh, this, true);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.list_swipeRefreshLayout);
        mRecycleView = (RecyclerView) findViewById(R.id.list_recycle_view);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(mCurrentState == ACTION_IDLE && mIsLoadMoreEnabled
                        && checkIfLoadMore()){
                    mAdapter.showLoadMoreFooter(true);
                    mCurrentState = ACTION_LOAD_MORE;
                    //把下拉刷新的刷新给禁止掉
//                    mSwipeRefreshLayout.setEnabled(false);
                    mListener.onRresh(ACTION_LOAD_MORE);
                }
            }
        });
    }

    /**
     * 判断是否可以加载更多
     * @return
     */
    private boolean checkIfLoadMore() {
        int lastVisiblePosition = mManager.findLastVisiblePosition();
        int itemCount = mManager.getLayoutManager().getItemCount(); //item的总个数
        return itemCount - lastVisiblePosition < 3;

    }


    public void setCurrentState(int mCurrentState) {
        this.mCurrentState = mCurrentState;
    }

    public void setIsLoadMoreEnabled(boolean mIsLoadMoreEnabled) {
        this.mIsLoadMoreEnabled = mIsLoadMoreEnabled;
    }

    public void setIsPullToRreshEnabled(boolean mIsPullToRreshEnabled) {
        this.mIsPullToRreshEnabled = mIsPullToRreshEnabled;
    }

    public void setManager(ILayoutManager mManager) {
        this.mManager = mManager;
        mRecycleView.setLayoutManager(mManager.getLayoutManager());
    }

    public void setRecycleRefreshListener(OnRecycleRefreshListener mListener) {
        this.mListener = mListener;
    }

    @Override
    public void onRefresh() {
        mCurrentState = ACTION_PULL_TO_REFRESH;
        mListener.onRresh(ACTION_PULL_TO_REFRESH);
    }

    public interface OnRecycleRefreshListener{

        void onRresh(int action);

    }

    public void addItemDecoration(RecyclerView.ItemDecoration decoration) {
        if (decoration != null) {
            mRecycleView.addItemDecoration(decoration);
        }
    }

    public void setAdapter(RecyclerView.Adapter adapter) {
        mRecycleView.setAdapter(adapter);
    }

    public void setRefreshing() {
        if(null != mSwipeRefreshLayout){

            mSwipeRefreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefreshLayout.setRefreshing(true);
                    onRefresh();
                }
            });
        }
    }
    public void onRefreshCompleted() {
        switch (mCurrentState) {
            case ACTION_PULL_TO_REFRESH:
                mSwipeRefreshLayout.setRefreshing(false);
                break;
            case ACTION_LOAD_MORE:
//                if (mIsPullToRreshEnabled) {
//                    mSwipeRefreshLayout.setEnabled(true);
//                }
                mAdapter.showLoadMoreFooter(false);
                break;
        }
        mCurrentState = ACTION_IDLE; //状态及时跟新
    }

}
